<?php

namespace fztadmin\modules\user\controllers;

use common\models\db\User;
use common\Tools;
use fztadmin\modules\user\models\forms\Custom;
use fztadmin\modules\user\models\forms\CustomBankCard;
use fztadmin\modules\user\models\forms\SetPayPassword;
use fztadmin\modules\user\models\forms\SetPhone;
use fztadmin\modules\user\models\forms\UpdatePasswrod;
use wodrow\yii2wtools\tools\Model;
use yii\web\Controller;

class CenterController extends Controller
{
    public function actionIndex()
    {
        return $this->render('index');
    }

    public function actionUpdatePassword()
    {
        $model = new UpdatePasswrod();
        if ($model->load(\Yii::$app->request->post()) && $model->validate()){
            if ($model->updatePassword()){
                \Yii::$app->session->addFlash("success", "修改密码成功");
                return $this->redirect(['/site/index']);
            }
        }
        return $this->renderAjax('update-password', ['model' => $model]);
    }

    public function actionBindNewCard()
    {
        $fzt = \Yii::$app->fenZhangTong;
        $model = new CustomBankCard();
        $model->plat_no = $fzt->platNo;
        if ($model->load(\Yii::$app->request->post()) && $model->validate()) {
            if ($model->bindNew($fzt)) {
                $model->setScenario($model::SCENARIO_CHECKSMS);
                return $this->redirect(['/user/center/bind-new-card-check', 'json' => Tools::toJson($model->toArray()), 'oriTransId' => $fzt->transId]);
            }
        }
        return $this->render('bind-new-card', ['model' => $model]);
    }

    public function actionBindNewCardCheck($json, $oriTransId)
    {
        $data = Tools::isJson($json);
        $fzt = \Yii::$app->fenZhangTong;
        $model = new CustomBankCard();
        $model->load($data, "");
        $model->oriTransId = $oriTransId;
        $model->setScenario($model::SCENARIO_CHECKSMS);
        if ($model->load(\Yii::$app->request->post()) && $model->validate()) {
            if ($model->bindNewCheck($fzt)) {
                return $this->redirect(['/site/index']);
            }
        }
        return $this->render('bind-new-card-check', ['model' => $model]);
    }

    public function actionSetSettlementCard($card_id)
    {
        $model = CustomBankCard::findOne($card_id);
        $fzt = \Yii::$app->fenZhangTong;
        if ($model->setSettlementCard($fzt)) {
            return $this->redirect($fzt->resp_arr['retUrl']);
        }
        return $this->redirect('/site/index');
    }

    public function actionUpdateMobile($custom_id)
    {
        $model = Custom::findOne($custom_id);
        $fzt = \Yii::$app->fenZhangTong;
        if ($model->updateMobile($fzt)) {
            return $this->redirect($fzt->resp_arr['retUrl']);
        }
        return $this->redirect('/site/index');
    }

    public function actionUpdateLocalMobile($custom_id)
    {
        $model = Custom::findOne($custom_id);
        $model->mobile_no = "";
        if ($model->load(\Yii::$app->request->post()) && $model->validate()) {
            if ($model->save()) {
                \Yii::$app->session->addFlash("success", "手机号更新成功");
                return $this->redirect(['/site/index']);
            }else{
                \Yii::$app->session->addFlash("error", Model::getModelError($model));
            }
        }
        return $this->render('upload-local-mobile', ['model' => $model]);
    }

    public function actionUpdatePayPassword($custom_id)
    {
        $model = Custom::findOne($custom_id);
        $fzt = \Yii::$app->fenZhangTong;
        if ($model->updatePayPassword($fzt)) {
            return $this->redirect($fzt->resp_arr['retUrl']);
        }
        return $this->redirect('/site/index');
    }

    public function actionSetPayPassword()
    {
        /**
         * @var User $user
         */
        $user = \Yii::$app->user->identity;
        if (!$user->phone){
            return "你必须绑定手机号才能设置支付密码";
        }
        $model = new SetPayPassword();
        if ($model->load(\Yii::$app->request->post()) && $model->validate()){
            if ($model->updatePassword()){
                \Yii::$app->session->addFlash("success", "修改密码成功");
                return $this->redirect(['index']);
            }
        }
        return $this->renderAjax('set-pay-password', ['model' => $model]);
    }

    public function actionSetPhone()
    {
        /**
         * @var User $user
         */
        $user = \Yii::$app->user->identity;
        $model = new SetPhone();
        return $this->renderAjax('set-phone', [
            'model' => $model
        ]);
    }

    public function actionSendPhoneCode()
    {}
}